package net.homeip.jcperera.nu.model.entity;

import java.io.Serializable;

import javax.inject.Named;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import net.homeip.jcperera.nu.core.CoreBase;
import net.homeip.jcperera.nu.core.dom.IntegrityCheckable;
import net.homeip.jcperera.nu.core.msg.BaseMessage;

/**
 * Base Entity of Framework, base for all entities
 * 
 * @name Base Entity class
 * @author julio.c.perera@gmail.com
 * @application nu
 * @since 0.1.0.0
 */

@Named(BaseEntity.componentName)
@MappedSuperclass
public class BaseEntity extends CoreBase implements Serializable, IntegrityCheckable
{
    // Required to implement Serializable interface
    private static final long  serialVersionUID = -6164097772333422372L;

    // CoreBase properties, for easy use from other components, as well as logging
    public static final int    runtimeLevel     = 1;
    public static final String moduleName       = "model";
    public static final String componentName    = "nu_model_entity_baseentity";

    // Associated type class, not persistent attribute
    @Transient
    private Type               type             = null;

    /**
     * This method retrieves all identity checks associated to the className of the instance and executes them one by
     * one, returning any messages that should occur during the execution
     */
    public void checkIntegrity() throws BaseMessage
    {

    }

    /**
     * Sets the type of this Entity, needed only because we have persistence timing problems with some attributes, in
     * the case of new Types, they persist but without assign the type first, because persistence automatically happens
     * via {@link TypeManager} and before setting the <code>type</code> attribute
     * 
     * @see #getType()
     * @param newType
     */
    protected void setType(Type newType)
    {
        this.type = newType;
    }

    /**
     * Gets the system entity {@link Type} associated to this {@link BaseEntity}
     * 
     * @return the instance {@link Type}
     */
    public Type getType() throws BaseMessage
    {
        // TODO: check here
        // if (this.type == null) this.type = TypeManager.getInstance().getTypeByEntity(this);
        return this.type;
    }
}
